//
//  UIButton+AG.swift
//  ASW
//
//  Created by zhuxuhong on 2020/11/22.
//

import UIKit
import BWToolKit
import Kingfisher

extension UIButton {
    public func bw_loadImage(
        _ url: String?,
        placeholderImage: UIImage? = nil,
        for state: UIControl.State = .normal,
        completion: BlockTT<UIImage?, Error?>? = nil
    ) {
        guard let url = url, let aURL = URL(string: url) else {
            return
        }
        let w = min(bounds.width, bounds.height)
        KF.url(aURL)
            .loadDiskFileSynchronously()
            .placeholder(placeholderImage)
            .transition(.fade(0.25))
            .appendProcessor(ResizingImageProcessor(referenceSize: .init(width: w, height: w), mode: .aspectFit))
            .onFailure{ completion?(nil, $0) }
            .onSuccess { completion?($0.image, nil) }
            .set(to: self, for: state)
    }
}

extension UIButton {
    func bw_setEnabled(_ enabled: Bool, alpha: CGFloat = 0.2) {
        isUserInteractionEnabled = enabled
        if let color = backgroundColor {
            backgroundColor = enabled ? color.a10 : color.alpha(alpha)
        }
    }
}

extension UIButton {
    struct EventASKey {
        static var touchUpInside = "touchUpInside"
    }
    
    public func bw_tapped(_ handler: @escaping BlockT<UIButton>) {
        Objcer.setASValue(handler, for: self, key: &EventASKey.touchUpInside, policy: .copy)
        addTarget(self, action: #selector(_touchUpInside), for: .touchUpInside)
    }
    
    static func bw_system(
        title: String? = nil,
        image: UIImage? = nil,
        tintColor: UIColor = .black,
        handler: @escaping BlockT<UIButton>
    ) -> UIButton
    {
        let btn = UIButton(type: .system)
        
        btn.bw_setTitle(title)
        btn.bw_setImage(image)
        btn.tintColor = tintColor
        btn.sizeToFit()
        btn.bw_tapped(handler)
        
        return btn
    }
    
    @objc private func _touchUpInside(){
        if let handler = Objcer.getASValue(for: self, key: &EventASKey.touchUpInside) as? BlockT<UIButton> {
            handler(self)
        }
    }
}

extension UIButton {
    public func bw_setBackgroundImage(_ image: UIImage?){
        setBackgroundImage(image, for: .normal)
    }
    
    public func bw_setImage(_ image: UIImage?){
        setImage(image, for: .normal)
    }
    
    public func bw_setTitle(_ title: String?){
        titleLabel?.text = title
        setTitle(title, for: .normal)
        sizeToFit()
        titleLabel?.sizeToFit()
    }
    
    public func bw_setAttrTitle(_ title: NSAttributedString?){
        titleLabel?.attributedText = title
        setAttributedTitle(title, for: .normal)
    }
    
    public static func bw_with(type: ButtonType = .system,
                               image: UIImage? = nil,
                               title: String?,
                               backgroundImage: UIImage? = nil,
                               target: Any, action: Selector) -> UIButton
    {
        let btn = UIButton(type: .system)
        btn.bw_setImage(image)
        btn.bw_setTitle(title)
        btn.bw_setBackgroundImage(backgroundImage)
        btn.addTarget(target, action: action, for: .touchUpInside)
        return btn
    }
}
